//
// Created by 相开原 on 2022/4/13.
//

#include "print.h"


void print_iShop() {
    printf("\n"
           "    ____    __  __                    \n"
           " __/\\  _`\\ /\\ \\/\\ \\                   \n"
           "/\\_\\ \\,\\L\\_\\ \\ \\_\\ \\    ___   _____   \n"
           "\\/\\ \\/_\\__ \\\\ \\  _  \\  / __`\\/\\ '__`\\ \n"
           " \\ \\ \\/\\ \\L\\ \\ \\ \\ \\ \\/\\ \\L\\ \\ \\ \\L\\ \\\n"
           "  \\ \\_\\ `\\____\\ \\_\\ \\_\\ \\____/\\ \\ ,__/\n"
           "   \\/_/\\/_____/\\/_/\\/_/\\/___/  \\ \\ \\/ \n"
           "                                \\ \\_\\ \n"
           "                                 \\/_/ \n");
}

void print_welcome() {
    puts("============================= Welcome =================================");
}

void print_menu(char *input) {
    char menu[256];
    char *token;
    strcpy(menu, input);
    token = strtok(menu, "\t");
    while (token != NULL) {
        char temp[2] = {*(token + 1), '\0'};
        terminal(temp, 1, 30, 46);
        terminal(token + 3, 0, 30, 47);
        token = strtok(NULL, "\t");
    }
    printf("\uE0BC\n");
    terminal(">", 1, 32, -1);
}

void print_number(char *number, int width) {
    if (width < 4) width = 4;
    char temp[16] = {'\0'};
    int space = width - strlen(number);
    while (space-- > 0) strcat(temp, " ");
    strcat(temp, number);
    terminal(temp, 1, 30, 47);
}

void print_fruitStored(pointer *p) {
    int all = 0, number = 0, width;
    for (pointer *i = p->next; i != NULL; i = i->next, all++);
    for (width = 0; all != 0; all /= 10, width++);
    print_number("编号", 4);
    terminal("名称             品种             单价    数量      保质期  到期时间 ", 0, 30, 47);
    printf("\n");

    for (p = p->next; p != NULL; p = p->next) {
        fruit *item = p->p_fruit;
        char temp[256];
        sprintf(temp, "%d ", ++number);
        print_number(temp, width);
        sprintf(temp, "%-16s %-16s %-7.2lf %-9.2lf %4d天  %4d/%02d/%02d", item->name, item->type, item->unitPrice,
                item->kilo,
                item->lifetime, item->life.year, item->life.month, item->life.day);
        terminal(temp, 0, 37, 40);
        printf("\n");
    }
}

void print_fruitOnShelf(pointer *p) {
    int all = 0, number = 0, width;
    for (pointer *i = p->next; i != NULL; i = i->next, all++);
    for (width = 0; all != 0; all /= 10, width++);
    print_number("编号", 4);
    terminal("名称             品种             单价    数量      价格模式 等级  到期时间     上架时间          ", 0, 30, 47);
    printf("\n");

    for (p = p->next; p != NULL; p = p->next) {
        fruit *item = p->p_fruit;
        char temp[256];
        sprintf(temp, "%d ", ++number);
        print_number(temp, width);
        sprintf(temp, "%-16s %-16s %-7.2lf %-9.2lf %-10s %-6s %4d/%02d/%02d   %4d/%02d/%02d-%02d:%02d:%02d",
                item->name, item->type, item->unitPrice, item->kilo, priceMode_to_text(item->mode),
                fruitLevel_to_text(item->level),
                item->life.year, item->life.month, item->life.day,
                item->shelfTime.year, item->shelfTime.month, item->shelfTime.day,
                item->shelfTime.hour, item->shelfTime.minute, item->shelfTime.second);
        terminal(temp, 0, 37, 40);
        printf("\n");
    }
}

void print_purchase(pointer *p) {
    char temp[256];
    int all = 0, number = 0, width;
    for (pointer *i = p->next; i != NULL; i = i->next, all++);
    for (width = 0; all != 0; all /= 10, width++);
    print_number("编号", 4);
    terminal("名称             品种             单价    数量      总价      进货商           进货时间          ", 0, 30, 47);
    printf("\n");

    for (p = p->next; p != NULL; p = p->next) {
        purchaseMessage *item = p->p_purchase;
        sprintf(temp, "%d", ++number);
        print_number(temp, width);
        sprintf(temp, "%-16s %-16s %-7.2lf %-9.2lf %-9.2lf %-16s %4d/%02d/%02d-%02d:%02d:%02d",
                item->name, item->type, item->unitPrice, item->kilo, item->totalPrice, item->supplier,
                item->purchaseTime.year, item->purchaseTime.month, item->purchaseTime.day,
                item->purchaseTime.hour, item->purchaseTime.minute, item->purchaseTime.second);
        terminal(temp, 0, 37, 40);
        printf("\n");
    }
}

void print_sell(pointer *p) {
    int all = 0, number = 0, width;
    for (pointer *i = p->next; i != NULL; i = i->next, all++);
    for (width = 0; all != 0; all /= 10, width++);
    print_number("编号", 4);
    terminal("购物件数 总价      顾客信息         销售时间                                                       ", 0, 30, 47);
    printf("\n");

    for (p = p->next; p != NULL; p = p->next) {
        sellMessage *item = p->p_sell;
        char temp[256];
        sprintf(temp, "%d ", ++number);
        print_number(temp, width);
        sprintf(temp, "%-8d %-9.2lf %-16s %4d/%02d/%02d-%02d:%02d:%02d                                            ",
                item->number, item->totalPrice, item->customer,
                item->sellTime.year, item->sellTime.month, item->sellTime.day,
                item->sellTime.hour, item->sellTime.minute, item->sellTime.second);
        terminal(temp, 0, 37, 40);
        printf("\n");

        if (p->p_sell->head != NULL) {
            int total = 0, num = 0;
            for (pointer *i = p->next; i != NULL; i = i->next, total++);
            print_number("内容", 4);
            terminal("名称             品种             单价    数量      价格模式 等级  到期时间     上架时间           ", 0, 30, 43);
            printf("\n");
            for (fruit *tp = p->p_sell->head->next; tp != NULL; tp = tp->next) {
                char content[256];
                sprintf(content, " -> ");
                print_number(content, width);
                sprintf(content, "%-16s %-16s %-7.2lf %-9.2lf %-10s %-6s %4d/%02d/%02d   %4d/%02d/%02d-%02d:%02d:%02d",
                        tp->name, tp->type, tp->unitPrice, tp->kilo,
                        priceMode_to_text(tp->mode), fruitLevel_to_text(tp->level),
                        tp->life.year, tp->life.month, tp->life.day,
                        tp->shelfTime.year, tp->shelfTime.month, tp->shelfTime.day,
                        tp->shelfTime.hour, tp->shelfTime.minute, tp->shelfTime.second);
                terminal(content, 0, 30, 46);
                printf("\n");
            }
        }
    }
}

void print_user(pointer *p) {
    int all = 0, number = 0, width;
    for (pointer *i = p->next; i != NULL; i = i->next, all++);
    for (width = 0; all != 0; all /= 10, width++);
    print_number("编号", 4);
    terminal("账户名     账户类别   销售额      创建时间  ", 0, 30, 47);
    printf("\n");

    for (p = p->next; p != NULL; p = p->next) {
        account *item = p->p_account;
        char temp[256];
        sprintf(temp, "%d ", ++number);
        print_number(temp, width);
        sprintf(temp, "%-8s   %-8s     %-9.2lf   %4d/%02d/%02d",
                item->username, get_userType(item), item->money,
                item->addTime.year, item->addTime.month, item->addTime.day);
        terminal(temp, 0, 37, 40);
        printf("\n");
    }
}

void print_VIP(pointer *p) {
    int all = 0, number = 0, width;
    for (pointer *i = p->next; i != NULL; i = i->next, all++);
    for (width = 0; all != 0; all /= 10, width++);
    print_number("编号", 4);
    terminal("账户名     账户类别   消费额      创建时间  ", 0, 30, 47);
    printf("\n");

    for (p = p->next; p != NULL; p = p->next) {
        account *item = p->p_account;
        char temp[256];
        sprintf(temp, "%d ", ++number);
        print_number(temp, width);
        sprintf(temp, "%-8s   %-8c     %-9.2lf   %4d/%02d/%02d",
                item->username, item->type, item->money,
                item->addTime.year, item->addTime.month, item->addTime.day);
        terminal(temp, 0, 37, 40);
        printf("\n");
    }
}

void print_receipt(fruit *p, double totalMoney) {
    terminal("名称     品种     单价    数量           小计", 0, 30, 47);
    printf("\n");

    for (p = p->next; p != NULL; p = p->next) {
        fruit *item = p;
        char temp[256];
        sprintf(temp, "%-8s %-8s %-7.2lf %-9.2lf %9.2lf",
                item->name, item->type, item->unitPrice, item->kilo, item->unitPrice * item->kilo);
        terminal(temp, 0, 37, 40);
        printf("\n");
    }
    char temp[64];
    struct time_item now;
    get_timeItem(&now);
    sprintf(temp, "总价：%.2lf", totalMoney);
    terminal(temp, 0, 37, 40);
    printf("\n");
    sprintf(temp, "时间：%4d/%02d/%02d %02d:%02d:%02d",
            now.year, now.month, now.day, now.hour, now.minute, now.second);
    terminal(temp, 0, 37, 40);
    printf("\n");
}